home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / dev / obero / Interfaces3_4.lha / Interfaces / IFFParse.mod < prev    next >
Text File  |  1994-03-05  |  16KB  |  354 lines

  1. (*
  2. (*
  3. **  Amiga Oberon Interface Module:
  4. **  $VER: IFFParse.mod 40.15 (28.12.93) Oberon 3.0
  5. **
  6. **   © 1993 by Fridtjof Siebert
  7. **   updated for V39, V40 by hartmut Goebel
  8. *)
  9. *)
  10.  
  11. MODULE IFFParse;
  12.  
  13. IMPORT
  14.   e * := Exec,
  15.   cb* := ClipBoard,
  16.   u * := Utility,
  17.   y * := SYSTEM;
  18.  
  19. CONST
  20.   iffparseName * = "iffparse.library";
  21.  
  22. (* Struct associated with an active IFF stream.
  23.  * "IFFHandle.stream" is a value used by the client's read/write/seek functions -
  24.  * it will not be accessed by the library itself and can have any value
  25.  * (could even be a pointer or a BPTR).
  26.  *
  27.  * This structure can only be allocated by iffparse.library
  28.  *)
  29.  
  30. TYPE
  31.  
  32.   IFFHandlePtr * = UNTRACED POINTER TO IFFHandle;
  33.   IFFHandle * = STRUCT
  34.     stream * : LONGINT;
  35.     flags * : LONGSET;
  36.     depth * : LONGINT;      (* Depth of context stack *)
  37.   END;
  38.  
  39. CONST
  40.  
  41. (* bit masks for "IFFHandle.flags" field *)
  42.   read      * = LONGSET{};           (* read mode - default *)
  43.   write     * = LONGSET{0};          (* write mode *)
  44.   rwBits    * = read + write;        (* read/write bits *)
  45.   fSeek     * = LONGSET{1};          (* forward seek only *)
  46.   rSeek     * = LONGSET{2};          (* random seek *)
  47.   reserved  * = LONGSET{16..31};     (* Don't touch these bits *)
  48.  
  49.  
  50. TYPE
  51. (*****************************************************************************)
  52. (* When the library calls your stream handler, you'll be passed a pointer
  53.  * to this structure as the "message packet".
  54.  *)
  55.   IFFStreamCmdPtr * = UNTRACED POINTER TO IFFStreamCmd;
  56.   IFFStreamCmd * = STRUCT
  57.     command * : LONGINT;     (*  Operation to be performed (IFFCMD_) *)
  58.     buf * : e.APTR;          (*  Pointer to data buffer              *)
  59.     nBytes * : LONGINT;      (*  Number of bytes to be affected      *)
  60.   END;
  61.  
  62.  
  63. (*****************************************************************************)
  64.  
  65. (* A node associated with a context on the iff_Stack.  Each node
  66.  * represents a chunk, the stack representing the current nesting
  67.  * of chunks in the open IFF file.  Each context node has associated
  68.  * local context items in the (private) LocalItems list.  The ID, type,
  69.  * size and scan values describe the chunk associated with this node.
  70.  *
  71.  * This structure can only be allocated by iffparse.library
  72.  *)
  73.   ContextNodePtr * = UNTRACED POINTER TO ContextNode;
  74.   ContextNode * = STRUCT (node *: e.MinNode)
  75.     id   *: LONGINT;
  76.     type *: LONGINT;
  77.     size *: LONGINT;        (*  Size of this chunk             *)
  78.     scan *: LONGINT;        (*  # of bytes read/written so far *)
  79.   END;
  80.  
  81.  
  82. (*****************************************************************************)
  83.  
  84. (* Local context items live in the ContextNode's.  Each class is identified
  85.  * by its lci_Ident code and has a (private) purge vector for when the
  86.  * parent context node is popped.
  87.  *
  88.  * This structure can only be allocated by iffparse.library
  89.  *)
  90.   LocalContextItemPtr * = UNTRACED POINTER TO LocalContextItem;
  91.   LocalContextItem * = STRUCT (node *: e.MinNode)
  92.     id    *: LONGINT;
  93.     type  *: LONGINT;
  94.     ident *: LONGINT;
  95.   END;
  96.  
  97.  
  98. (*****************************************************************************)
  99.  
  100. (* StoredProperty: a local context item containing the data stored
  101.  * from a previously encountered property chunk.
  102.  *)
  103.   StoredPropertyPtr * = UNTRACED POINTER TO StoredProperty;
  104.   StoredProperty * = STRUCT
  105.     size * : LONGINT;
  106.     data * : e.APTR;
  107.   END;
  108.  
  109. (*****************************************************************************)
  110.  
  111. (* Collection Item: the actual node in the collection list at which
  112.  * client will look.  The next pointers cross context boundaries so
  113.  * that the complete list is accessable.
  114.  *)
  115.   CollectionItemPtr * = UNTRACED POINTER TO CollectionItem;
  116.   CollectionItem * = STRUCT
  117.     next * : CollectionItemPtr;
  118.     size * : LONGINT;
  119.     data * : e.APTR;
  120.   END;
  121.  
  122. (*****************************************************************************)
  123.  
  124. (* Structure returned by OpenClipboard().  You may do CMD_POSTs and such
  125.  * using this structure.  However, once you call OpenIFF(), you may not
  126.  * do any more of your own I/O to the clipboard until you call CloseIFF().
  127.  *)
  128.   ClipboardHandlePtr * = UNTRACED POINTER TO ClipboardHandle;
  129.   ClipboardHandle * = STRUCT (req * : cb.IOClipReq)
  130.     cbport * : e.MsgPort;
  131.     satisfyPort * : e.MsgPort;
  132.   END;
  133.  
  134. CONST
  135. (*****************************************************************************)
  136.  
  137. (* IFF return codes.  Most functions return either zero for success or
  138.  * one of these codes.  The exceptions are the read/write functions which
  139.  * return positive values for number of bytes or records read or written,
  140.  * or a negative error code.  Some of these codes are not errors per sae,
  141.  * but valid conditions such as EOF or EOC (End of Chunk).
  142.  *)
  143.   errEOF          * = -1;     (*  Reached logical end of file *)
  144.   errEOC          * = -2;     (*  About to leave context      *)
  145.   errNoScope      * = -3;     (*  No valid scope for property *)
  146.   errNoMem        * = -4;     (*  Internal memory alloc failed*)
  147.   errRead         * = -5;     (*  Stream read error           *)
  148.   errWrite        * = -6;     (*  Stream write error          *)
  149.   errSeek         * = -7;     (*  Stream seek error           *)
  150.   errMangled      * = -8;     (*  Data in file is corrupt     *)
  151.   errSyntax       * = -9;     (*  IFF syntax error            *)
  152.   errNotIFF       * = -10;    (*  Not an IFF file             *)
  153.   errNoHook       * = -11;    (*  No call-back hook provided  *)
  154.   return2Client   * = -12;    (*  Client handler normal return*)
  155.  
  156. (*****************************************************************************)
  157.  
  158. (* Universal IFF identifiers *)
  159.   idFORM   * = y.VAL(LONGINT,"FORM");
  160.   idLIST   * = y.VAL(LONGINT,"LIST");
  161.   idCAT    * = y.VAL(LONGINT,"CAT ");
  162.   idPROP   * = y.VAL(LONGINT,"PROP");
  163.   idNULL   * = y.VAL(LONGINT,"    ");
  164.  
  165. (* Identifier codes for universally recognized local context items *)
  166.   lciPROP         * = y.VAL(LONGINT,"prop");
  167.   lciCOLLECTION   * = y.VAL(LONGINT,"coll");
  168.   lciENTRYHANDLER * = y.VAL(LONGINT,"enhd");
  169.   lciEXITHANDLER  * = y.VAL(LONGINT,"exhd");
  170.  
  171.  
  172. (*****************************************************************************)
  173.  
  174. (* Control modes for ParseIFF() function *)
  175.   parseScan       * = 0;
  176.   parseStep       * = 1;
  177.   parseRawStep    * = 2;
  178.  
  179.  
  180. (*****************************************************************************)
  181.  
  182. (* Control modes for StoreLocalItem() function *)
  183.   sliRoot         * = 1;      (*  Store in default context       *)
  184.   sliTop          * = 2;      (*  Store in current context       *)
  185.   sliProp         * = 3;      (*  Store in topmost FORM or LIST  *)
  186.  
  187.  
  188. (*****************************************************************************)
  189.  
  190. (* Magic value for writing functions. If you pass this value in as a size
  191.  * to PushChunk() when writing a file, the parser will figure out the
  192.  * size of the chunk for you. If you know the size, is it better to
  193.  * provide as it makes things faster.
  194.  *)
  195.  
  196.   sizeUnknown     * = -1;
  197.  
  198.  
  199. (*****************************************************************************)
  200.  
  201. (* Possible call-back command values *)
  202.   cmdInit     * = 0;       (*  Prepare the stream for a session    *)
  203.   cmdCleanup  * = 1;       (*  Terminate stream session            *)
  204.   cmdRead     * = 2;       (*  Read bytes from stream              *)
  205.   cmdWrite    * = 3;       (*  Write bytes to stream               *)
  206.   cmdSeek     * = 4;       (*  Seek on stream                      *)
  207.   cmdEntry    * = 5;       (*  You just entered a new context      *)
  208.   cmdExit     * = 6;       (*  You're about to leave a context     *)
  209.   cmdPurgeLCI * = 7;       (*  Purge a LocalContextItem            *)
  210.  
  211. VAR
  212.   base * : e.LibraryPtr;
  213.  
  214. (*--- functions in V36 or higher (Release 2.0) ---*)
  215.  
  216. (*------ Basic functions ------*)
  217.  
  218. PROCEDURE AllocIFF          *{base,- 30}(): IFFHandlePtr;
  219. PROCEDURE OpenIFF           *{base,- 36}(iff{8}            : IFFHandlePtr;
  220.                                          rwMode{0}         : LONGSET): LONGINT;
  221. PROCEDURE ParseIFF          *{base,- 42}(iff{8}            : IFFHandlePtr;
  222.                                          control{0}        : LONGINT): LONGINT;
  223. PROCEDURE CloseIFF          *{base,- 48}(iff{8}            : IFFHandlePtr);
  224. PROCEDURE FreeIFF           *{base,- 54}(ifff{8}           : IFFHandlePtr);
  225.  
  226. (*------ Read/Write functions ------*)
  227.  
  228. PROCEDURE ReadChunkBytes    *{base,- 60}(iff{8}            : IFFHandlePtr;
  229.                                          VAR buf{9}        : ARRAY OF e.BYTE;
  230.                                          numBytes{0}       : LONGINT): LONGINT;
  231. PROCEDURE WriteChunkBytes   *{base,- 66}(iff{8}            : IFFHandlePtr;
  232.                                          buf{9}            : ARRAY OF e.BYTE;
  233.                                          numBytes{0}       : LONGINT): LONGINT;
  234. PROCEDURE ReadChunkRecords  *{base,- 72}(iff{8}            : IFFHandlePtr;
  235.                                          VAR buf{9}        : ARRAY OF e.BYTE;
  236.                                          bytesPerRecord{0} : LONGINT;
  237.                                          numRecords{1}     : LONGINT): LONGINT;
  238. PROCEDURE WriteChunkRecords *{base,- 78}(iff{8}            : IFFHandlePtr;
  239.                                          buf{9}            : ARRAY OF e.BYTE;
  240.                                          bytesPerRecord{0} : LONGINT;
  241.                                          numRecords{1}     : LONGINT): LONGINT;
  242.  
  243. (*------ Context entry/exit ------*)
  244.  
  245. PROCEDURE PushChunk         *{base,- 84}(iff{8}            : IFFHandlePtr;
  246.                                          type{0}           : LONGINT;
  247.                                          id{1}             : LONGINT;
  248.                                          size{2}           : LONGINT): LONGINT;
  249. PROCEDURE PopChunk          *{base,- 90}(iff{8}            : IFFHandlePtr): LONGINT;
  250.  
  251. (*------ Low-level handler installation ------*)
  252.  
  253. PROCEDURE EntryHandler      *{base,-102}(iff{8}            : IFFHandlePtr;
  254.                                          type{0}           : LONGINT;
  255.                                          id{1}             : LONGINT;
  256.                                          position{2}       : LONGINT;
  257.                                          handler{9}        : u.HookPtr;
  258.                                          object{10}        : e.APTR): LONGINT;
  259. PROCEDURE ExitHandler       *{base,-108}(iff{8}            : IFFHandlePtr;
  260.                                          type{0}           : LONGINT;
  261.                                          id{1}             : LONGINT;
  262.                                          position{2}       : LONGINT;
  263.                                          handler{9}        : u.HookPtr;
  264.                                          object{10}        : e.APTR): LONGINT;
  265.  
  266. (*------ Built-in chunk/property handlers ------*)
  267.  
  268. PROCEDURE PropChunk         *{base,-114}(iff{8}            : IFFHandlePtr;
  269.                                          type{0}           : LONGINT;
  270.                                          id{1}             : LONGINT): LONGINT;
  271. PROCEDURE PropChunks        *{base,-120}(iff{8}            : IFFHandlePtr;
  272.                                          propArray{9}      : ARRAY OF LONGINT;
  273.                                          numPairs{0}       : LONGINT): LONGINT;
  274. PROCEDURE StopChunk         *{base,-126}(iff{8}            : IFFHandlePtr;
  275.                                          type{0}           : LONGINT;
  276.                                          id{1}             : LONGINT): LONGINT;
  277. PROCEDURE StopChunks        *{base,-132}(iff{8}            : IFFHandlePtr;
  278.                                          propArray{9}      : ARRAY OF LONGINT;
  279.                                          numPairs{0}       : LONGINT): LONGINT;
  280. PROCEDURE CollectionChunk   *{base,-138}(iff{8}            : IFFHandlePtr;
  281.                                          type{0}           : LONGINT;
  282.                                          id{1}             : LONGINT): LONGINT;
  283. PROCEDURE CollectionChunks  *{base,-144}(iff{8}            : IFFHandlePtr;
  284.                                          propArray{9}      : ARRAY OF LONGINT;
  285.                                          numPairs{0}       : LONGINT): LONGINT;
  286. PROCEDURE StopOnExit        *{base,-150}(iff{8}            : IFFHandlePtr;
  287.                                          type{0}           : LONGINT;
  288.                                          id{1}             : LONGINT): LONGINT;
  289.  
  290. (*------ Context utilities ------*)
  291.  
  292. PROCEDURE FindProp          *{base,-156}(iff{8}            : IFFHandlePtr;
  293.                                          type{0}           : LONGINT;
  294.                                          id{1}             : LONGINT): StoredPropertyPtr;
  295. PROCEDURE FindCollection    *{base,-162}(iff{8}            : IFFHandlePtr;
  296.                                          type{0}           : LONGINT;
  297.                                          id{0}             : LONGINT): CollectionItemPtr;
  298. PROCEDURE FindPropContext   *{base,-168}(iff{8}            : IFFHandlePtr): ContextNodePtr;
  299. PROCEDURE CurrentChunk      *{base,-174}(iff{8}            : IFFHandlePtr): ContextNodePtr;
  300. PROCEDURE ParentChunk       *{base,-180}(contextNode{8}    : ContextNodePtr): ContextNodePtr;
  301.  
  302. (*------ LocalContextItem support functions ------*)
  303.  
  304. PROCEDURE AllocLocalItem    *{base,-186}(type{0}           : LONGINT;
  305.                                          id{1}             : LONGINT;
  306.                                          ident{2}          : LONGINT;
  307.                                          dataSize{3}       : LONGINT): LocalContextItemPtr;
  308. PROCEDURE LocalItemData     *{base,-192}(localItem{8}      : LocalContextItemPtr): e.APTR;
  309. PROCEDURE SetLocalItemPurge *{base,-198}(localItem{8}      : LocalContextItemPtr;
  310.                                          purgeHook{9}      : u.HookPtr);
  311. PROCEDURE FreeLocalItem     *{base,-204}(localItem{8}      : LocalContextItemPtr);
  312. PROCEDURE FindLocalItem     *{base,-210}(iff{8}            : IFFHandlePtr;
  313.                                          type{0}           : LONGINT;
  314.                                          id{1}             : LONGINT;
  315.                                          ident{2}          : LONGINT ): LocalContextItemPtr;
  316. PROCEDURE StoreLocalItem    *{base,-216}(iff{8}            : IFFHandlePtr;
  317.                                          localItem{9}      : LocalContextItemPtr;
  318.                                          position{0}       : LONGINT): LONGINT;
  319. PROCEDURE StoreItemInContext*{base,-222}(iff{8}            : IFFHandlePtr;
  320.                                          localItem{8}      : LocalContextItemPtr;
  321.                                          contextNode{10}   : ContextNodePtr);
  322.  
  323. (*------ IFFHandle initialization ------*)
  324.  
  325. PROCEDURE InitIFF           *{base,-228}(iff{8}            : IFFHandlePtr;
  326.                                          flags{0}          : LONGSET;
  327.                                          streamHook{9}     : u.HookPtr);
  328. PROCEDURE InitIFFasDOS      *{base,-234}(iff{8}            : IFFHandlePtr);
  329. PROCEDURE InitIFFasClip     *{base,-240}(iff{8}            : IFFHandlePtr);
  330.  
  331. (*------ Internal clipboard support ------*)
  332.  
  333. PROCEDURE OpenClipboard     *{base,-246}(unitNumber{0}     : LONGINT): ClipboardHandlePtr;
  334. PROCEDURE CloseClipboard    *{base,-252}(clipHandle{8}     : ClipboardHandlePtr);
  335.  
  336. (*------ Miscellaneous ------*)
  337.  
  338. PROCEDURE GoodID            *{base,-258}(id{0}             : LONGINT): LONGINT;
  339. PROCEDURE GoodType          *{base,-264}(type{0}           : LONGINT): LONGINT;
  340. PROCEDURE IDtoStr           *{base,-270}(id{0}             : LONGINT;
  341.                                          VAR buf{8}        : ARRAY OF CHAR);
  342.  
  343.  
  344. (* $OvflChk- $RangeChk- $StackChk- $NilChk- $ReturnChk- $CaseChk- *)
  345.  
  346. BEGIN
  347.   base :=  e.OpenLibrary(iffparseName,37);
  348.  
  349. CLOSE
  350.   IF base#NIL THEN e.CloseLibrary(base) END;
  351.  
  352. END IFFParse.
  353.  
  354.